{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "25c90e9e",
   "metadata": {},
   "source": [
    "# Loading from LangChainHub\n",
    "\n",
    "This notebook covers how to load chains from [LangChainHub](https://github.com/hwchase17/langchain-hub)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "8b54479e",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chains import load_chain\n",
    "\n",
    "chain = load_chain(\"lc://chains/llm-math/chain.json\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4828f31f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new LLMMathChain chain...\u001b[0m\n",
      "whats 2 raised to .12\u001b[32;1m\u001b[1;3m\n",
      "Answer: 1.0791812460476249\u001b[0m\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'Answer: 1.0791812460476249'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "chain.run(\"whats 2 raised to .12\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8db72cda",
   "metadata": {},
   "source": [
    "Sometimes chains will require extra arguments that were not serialized with the chain. For example, a chain that does question answering over a vector database will require a vector database."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "aab39528",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.embeddings.openai import OpenAIEmbeddings\n",
    "from langchain.vectorstores import Chroma\n",
    "from langchain.text_splitter import CharacterTextSplitter\n",
    "from langchain import OpenAI, VectorDBQA"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "16a85d5e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Running Chroma using direct local API.\n",
      "Using DuckDB in-memory for database. Data will be transient.\n"
     ]
    }
   ],
   "source": [
    "from langchain.document_loaders import TextLoader\n",
    "\n",
    "loader = TextLoader(\"../../state_of_the_union.txt\")\n",
    "documents = loader.load()\n",
    "text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)\n",
    "texts = text_splitter.split_documents(documents)\n",
    "\n",
    "embeddings = OpenAIEmbeddings()\n",
    "vectorstore = Chroma.from_documents(texts, embeddings)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "6a82e91e",
   "metadata": {},
   "outputs": [],
   "source": [
    "chain = load_chain(\"lc://chains/vector-db-qa/stuff/chain.json\", vectorstore=vectorstore)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "efe9b25b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\" The president said that Ketanji Brown Jackson is a Circuit Court of Appeals Judge, one of the nation's top legal minds, a former top litigator in private practice, a former federal public defender, has received a broad range of support from the Fraternal Order of Police to former judges appointed by Democrats and Republicans, and will continue Justice Breyer's legacy of excellence.\""
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "query = \"What did the president say about Ketanji Brown Jackson\"\n",
    "chain.run(query)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f910a32f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
